<template>
    <div>
        <el-card style="margin-bottom:10px">
            <el-form :inline="true" :model="formInline" class="demo-form-inline">
                <el-form-item label="角色名称">
                    <el-input v-model="formInline.name" placeholder="请输入角色名称" clearable />
                </el-form-item>
                <el-form-item label="角色标识">
                    <el-input v-model="formInline.identity" placeholder="请输入角色标识" clearable />
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="formInline.status" placeholder="请选择状态" clearable>
                        <el-option label="开" :value="true" />
                        <el-option label="关" :value="false" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onQuery">查询</el-button>
                    <el-button @click="onReset">重置</el-button>
                </el-form-item>

            </el-form>
        </el-card>
        <el-card>
            <div style="display:flex;justify-content: space-between;">
                <h3>角色列表</h3>
                <el-button type="primary" @click="onAdd">新增角色</el-button>
            </div>
            <el-table :data="tableData" style="width: 100%">
                <el-table-column fixed prop="id" label="角色编号" width="180" />
                <el-table-column prop="name" label="角色名称" width="100" />
                <el-table-column prop="identity" label="角色标识" width="120" />
                <el-table-column prop="status" label="状态" width="100" />
                <el-table-column prop="remark" label="备注" show-overflow-tooltip />
                <el-table-column prop="createtime" label="创建时间" width="180" />
                <el-table-column fixed="right" label="操作" width="120">
                    <template #default="scope">
                        <el-button link type="primary" size="small" @click="onEdit(scope.row)">
                            修改
                        </el-button>
                        <el-popconfirm title="是否确认删除?" @confirm="onConfirm(scope.row)">
                            <template #reference>
                                <el-button link type="primary" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination background layout="->,total,prev, pager, next" :total="total"
                @current-change="handleChange" />

        </el-card>

        <el-dialog v-model="dialogFormVisible" :title="flag ? '新增角色' : '修改角色'" width="500">
            <el-form :model="form" label-width="80px">
                <el-form-item label="角色名称">
                    <el-input v-model="form.name" autocomplete="off" />
                </el-form-item>
                <el-form-item label="角色标识">
                    <el-input v-model="form.identity" autocomplete="off" />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input type="textarea" v-model="form.remark" autocomplete="off" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取消</el-button>
                    <el-button type="primary" @click="onSure">
                        确认
                    </el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
<script>
import request from '@/utils/request.js'
import { ElMessage } from 'element-plus'
export default {
    data() {
        return {
            formInline: {
                name: "",
                identity: '',
                status: '',
                pagination: 1,
                pageNum: 10
            },
            tableData: [
                {
                    id: '1',
                    name: 'Tom',
                    identity: '6666',
                    status: true,
                    remark: '2222222222222222222'
                },
            ],
            total: null,
            dialogFormVisible: false,
            form: {
                name: "",
                identity: "",
                remark: ""
            },
            flag: true, // 判断是新增还是删除
            rowId: ""
        }
    },
    created() {
        this.getList()
    },
    methods: {
        // 封装请求列表的函数
        getList() {
            request.get('/list', {
                params: this.formInline
            }).then(res => {
                console.log(res)
                this.tableData = res.data.data
                this.total = res.data.total
            })
        },
        handleChange(val) {
            console.log(val, 'val')
            this.formInline.pagination = val
            this.getList()
        },
        onQuery() {
            this.getList()
        },
        onReset() {
            // 清空form表单中的值
            this.formInline = {
                name: '',
                identity: '',
                status: '',
                pagination: 1,
                pageNum: 10
            }
            this.getList()
        },
        onAdd() {
            this.dialogFormVisible = true
            // 点击新增按钮时给flag设置为true
            this.flag = true
            // 清空form表单中的值
            this.form = {
                name: '',
                identity: '',
                remark: ''
            }
        },
        onSure() {
            // 判断： 如果是新增，调新增接口； 如果是修改，调修改接口
            // 如果flag是true,调新增接口； flag是false,调修改接口
            // 请求新增接口
            this.flag ?
                request.post('/add', this.form).then(res => {
                    if (res.data.code == 200) {
                        // 提示：新增成功
                        ElMessage({
                            message: '新增成功',
                            type: 'success',
                        })
                        // dialog关闭
                        this.dialogFormVisible = false
                        // 刷新列表
                        this.getList()
                    }
                })
                :
                request.post('/edit', {
                    // es6中的结构赋值语法
                    ...this.form,
                    id: this.rowId
                }).then(res => {
                    if (res.data.code == 200) {
                        // 提示：修改成功
                        ElMessage({
                            message: '修改成功',
                            type: 'success',
                        })
                        // dialog关闭
                        this.dialogFormVisible = false
                        // 刷新列表
                        this.getList()
                    }
                })
        },
        onConfirm(row) {
            console.log(row, '000')
            // 调删除接口
            request.post('/delete', { id: row.id }).then(res => {
                if (res.data.code == 200) {
                    // 提示删除成功
                    ElMessage({
                        message: '删除成功',
                        type: 'success',
                    })
                    // 刷新列表
                    this.getList()
                }
            })
        },
        onEdit(row) {
            // dialog弹框显示
            this.dialogFormVisible = true
            // 回显数据
            this.form = {
                name: row.name,
                identity: row.identity,
                remark: row.remark
            }
            // 点击编辑按钮时给flag设置为false
            this.flag = false
            // 存当前点击的id
            this.rowId = row.id
        }
    }
}

</script>

<style lang="scss" scoped>
.demo-form-inline .el-input {
    --el-input-width: 220px;
}

.demo-form-inline .el-select {
    --el-select-width: 220px;
}
</style>